/*******************************************************************
 * $ Id : ast_util.cpp  0000/00/00 00:00:00
 *
 *        swhors@naver.com
 *******************************************************************/

#include "config.h"

#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <time.h>

#include "constant.h"
#include "ast_util.h"

void remove_newline( char *aBufPtr , int aBufLen )
{
   int sCount = 0;
   
   while ( sCount < aBufLen )
   {
      if ( aBufPtr[sCount] == '\n' ||
           aBufPtr[sCount] == '\r' )
      {
         aBufPtr[sCount] = 0;
      }
      sCount ++;
   }
}

struct tm * current_time( struct tm *tval )
{
   time_t t,t1;
   t = time( &t1 );
   tval = localtime( &t );
   return tval;
}

int current_time_to_str( char *buf, int buflen )
{
   struct tm *tm = NULL;
   time_t t,t1;
   t = time( &t1 );
   tm = localtime( &t );
   strftime( buf, buflen, "%F %T", tm  );
   return strlen( buf );
}

// "yyyy-mm-dd HH:MM:SS" to struct tm
struct tm * time_by_str(char *buf, struct tm* tout)
{
   strptime( buf, "%F %T", tout );
   return tout;
}

void log_message( char *fmt, ... )
{
   char timebuf[32] = {0,}; 
   FILE *logfile    = NULL;
   va_list ap;
   
   logfile = fopen( LOG_FILE, "a+t" );
   
   if ( logfile == NULL )
   {
      return;
   }
   else
   {
      va_start( ap,fmt );
      current_time_to_str( timebuf, 32 );
      fprintf( logfile, "[%s] ", timebuf );
      vfprintf( logfile,fmt,ap );
      va_end( ap );
      fclose( logfile );
   }
}

////////////////////////////////////////////////////////////////////
